package org.example.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.pojo.Group;
import org.example.service.GroupService;
import org.example.utils.Result;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/group")
public class GroupController {
    @Autowired
    private GroupService groupService;
    @GetMapping("/list/{userId}")
    public Result groupList(@PathVariable("userId") Integer userId) {
            List<Group> groups = groupService.groupList(userId);
            return Result.success(groups);
    }
    @PostMapping
    public Result addGroup(@RequestBody Group group) {
            groupService.addGroup(group);
            return Result.success();
    }
    @DeleteMapping
    public Result deleteGroup(@RequestBody Group group) {
            groupService.delGroup(group);
            return Result.success();
    }
    @GetMapping("/topic")
    public Result topicList(){
        String url = "https://www.cangquyun.com/prod-api/browsing/topicList";
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        String json = response.getBody();
        return Result.success("操作成功",json);
    }
}
